package com.simple.gateway.center.basedata.common;

import com.alibaba.fastjson.JSON;

import java.io.Serializable;

/**
 * 项目: simple-gateway-core
 * <p>
 * 功能描述: 返回给前端的数据
 *
 * @author: WuChengXing
 * @create: 2023-08-07 11:54
 **/
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 532082634851875247L;

    private final String code;
    private final String info;
    private final T data;

    public Result(String code, String info, T data) {
        this.code = code;
        this.info = info;
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public String getInfo() {
        return info;
    }

    public T getData() {
        return data;
    }

    public static <R> Result<R> success(R data) {
        return new Result<>(ResponseCode.SUCCESS.getCode(),
                ResponseCode.SUCCESS.getInfo(), data);
    }

    public static <R> Result<R> error() {
        return new Result<>(ResponseCode.UN_ERROR.getCode(),
                ResponseCode.UN_ERROR.getInfo(), null);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
